module("myusart", package.seeall);
local myjson = require "myjson"


function usartInit()
    -- configure for 115200, 8N1, without echo
    uart.setup(0, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1)
    
    -- when '\r' chars is received.
    uart.on("data", "\r", function(data)
        print("receive from uart:", data)
        if string.find(data, 'quit') ~= nil then
            print('quit ok')
            uart.on("data") -- unregister callback function
        elseif string.find(data, 'ssid') ~= nil then
            print("json")
            myjson.readJson(data)
            myjson.WriteJson(myjson.ssid, myjson.pwd, myjson.mqttIP)
            node.restart()
        end
    end, 0)
end
